﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nectar.DAL.Common;
using P2PWeb.Common;
using P2PWeb.Models;

namespace P2PWeb.Controllers
{



    public class LendersController : Controller
    {
        //
        // GET: /Lenders/

        public ActionResult LendrsList(int pageIndex = 1)
        {
            string tab = Request.QueryString["tab"];
            DefaultJson dj = GetListData(tab, null, pageIndex);
            return View(dj);
        }

        public ActionResult Search(int pageIndex = 1)
        {
            string tab = Request.QueryString["tab"];
            Hashtable ht=new Hashtable();
            ht.Add("iapproveNo", Request.QueryString["iapproveNo"]);
            ht.Add("currRate", Request.QueryString["currRate"]);
            ht.Add("iapproveAmt", Request.QueryString["iapproveAmt"]);

            DefaultJson dj = GetListData(tab, ht, pageIndex);
            return PartialView("View1", dj);
        }

        public ActionResult LendrsDetail(int ID)
        {
            using (var context = new Repository<P2PModelContainer>())
            {

                var item = context.GetEntityByID<Campaign>(ID, new List<string>() { "Account", "account.AccountInfo"});
                List<Flight> flig_info = null;
                if (item == null || string.IsNullOrEmpty(item.AccountID.ToString()))
                {
                    flig_info = new List<Flight>();
                    return null;
                }
                else
                {
                    List<CampUpLoad> upload_list = new List<CampUpLoad>();
                    QueryParameter[] param = new QueryParameter[]{
                      new QueryParameter("CampaignID",ID)
                    };
                    upload_list = context.GetListOf<CampUpLoad>(" o.CampaignID=@CampaignID", param);
                    ViewData["upload_list"] = upload_list;
                    ViewData["Info"] = item.Info;

                    flig_info = context.GetEntityByAccountID<Flight>(item.AccountID);
                    OrderDetail dj = new OrderDetail()
                    {
                        Id = item.ID,
                        Title = item.Name,
                        LoanPerson = item.Account.Name,
                        IsJvb = false,
                        FinishRate = (Convert.ToDouble(item.RealAmount) / Convert.ToDouble(item.Amount)),
                        StartDate = item.StartDate,
                        DueDate = (TimeSpan)(item.EndDate - item.StartDate),
                        Money = item.Amount,
                        TimeSpan = item.StartDate.ToString("yyyy-MM-dd") + "至" + item.EndDate.ToString("yyyy-MM-dd"),
                        AnnualInterest = item.InterestRate,
                        SurplusMoney = item.Amount - item.RealAmount,
                        Status = item.Status,
                        Mortgage = EnumHelper.GetDisplayName((MortgageAttachment)item.Mortgage),
                        CalcType = EnumHelper.GetDisplayName((CalcType)item.CalcType),
                        account = item.Account,
                        flight = flig_info
                    };
                    return View(dj);
                }                
            }
        }


        public ActionResult Project(int ID)
        {
            using (var context = new Repository<P2PModelContainer>())
            {

                var item = context.GetEntityByID<Campaign>(ID, new List<string>() { "Account", "account.AccountInfo" });
                List<Flight> flig_info = null;
                if (item == null || string.IsNullOrEmpty(item.AccountID.ToString()))
                {
                    flig_info = new List<Flight>();
                    return null;
                }
                else
                {
                    List<CampUpLoad> upload_list = new List<CampUpLoad>();
                    QueryParameter[] param = new QueryParameter[]{
                      new QueryParameter("CampaignID",ID)
                    };
                    upload_list = context.GetListOf<CampUpLoad>(" o.CampaignID=@CampaignID", param);
                    ViewData["upload_list"] = upload_list;
                    ViewData["Info"] = item.Info;
                    flig_info = context.GetEntityByAccountID<Flight>(item.AccountID);
                    OrderDetail dj = new OrderDetail()
                    {
                        Id = item.ID,
                        Title = item.Name,
                        LoanPerson = item.Account.Name,
                        IsJvb = false,
                        FinishRate = (Convert.ToDouble(item.RealAmount) / Convert.ToDouble(item.Amount)),
                        DueDate = (TimeSpan)(item.DueDate - DateTime.Now),
                        StartDate = (DateTime)item.DueDate,
                        Money = item.Amount,
                        TimeSpan = item.StartDate.ToString("yyyy-MM-dd") + "至" + item.EndDate.ToString("yyyy-MM-dd"),
                        AnnualInterest = item.InterestRate,
                        SurplusMoney = item.Amount - item.RealAmount,
                        Status = item.Status,
                        Mortgage = EnumHelper.GetDisplayName((MortgageAttachment)item.Mortgage),
                        CalcType = EnumHelper.GetDisplayName((CalcType)item.CalcType),
                        account = item.Account,
                        flight = flig_info
                    };
                    return View(dj);
                }
            }
        }

        public ActionResult View1(int pageIndex = 1)
        {
            string tab = Request.QueryString["tab"];
            DefaultJson dj = GetListData(tab, null, pageIndex);
            return PartialView(dj);
        }

        //等 数据库建好后，在做联合查询
        private DefaultJson GetListData(string tab = "All", Hashtable ht = null,int pageIndex = 1)
        {


            DefaultJson dj = null;
            if (ht != null)
            {
                dj = DefaultJson.GetLendrsSearch(ht, pageIndex);
            }
            else
            {
                if (string.IsNullOrEmpty(tab) || tab.Equals("All"))
                    dj = DefaultJson.GetDemo(pageIndex);
                else if (tab.Equals("Success"))
                    dj = DefaultJson.GetDemo1(pageIndex);
                else if (tab.Equals("Raise"))
                    dj = DefaultJson.GetDemo(pageIndex);
            }



            return dj;
        }


    }
}
